import { _decorator, Component, Node } from 'cc';
const { ccclass, property } = _decorator;

// 洗牌的方法
export function shuffle(array) {
    // 随机选取一个与最后一个元素交换
    for (let i = array.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1));
        [array[i], array[j]] = [array[j], array[i]];
    }

    return array;
}

// 洗牌方法2
export function shuffle2(array) {
    let length: number = array.length, 
    temp: number, 
    randomIndex: number;
    
    while (length > 0) {
        randomIndex = Math.floor(Math.random() * length)
        temp = array[length - 1];
        array[length - 1] = array[randomIndex];
        array[randomIndex] = temp;

        length--;
    }

    return array;
}